﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Chapter_9__Recursion_
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            // 9.1
            // ---------------------------------------
            if (false)
            {
                long result = Ex_9_1.GetPossibleWays(30);
                bool test = true;
            }

            // 9.2
            // ---------------------------------------
            if (false)
            {
                long result = Ex_9_2.HowManyPaths(14, 14);
                bool test = true;
            }

            // 9.4
            // ---------------------------------------
            if (false)
            {
                SortableSortedSet<int> Set = new SortableSortedSet<int>();
                for (int i = 1; i <= 5; i++)
                    Set.Add(i);
                SortedSet<SortableSortedSet<int>> Sets = Ex_9_4.GetAllSubsets(Set);
                textBox1.Text = Ex_9_4.GetStringOfSubsets(Sets);
                this.Text = Sets.Count + " Subsets";

                bool test = true;
            }

            // 9.5
            // ---------------------------------------
            if (false)
            {
                SortedSet<string> Permutations = Ex_9_5.GetAllPermutations("andy");
                textBox1.Text = CreateString(Permutations);
                this.Text = Permutations.Count + " Permutations";
            }

            // 9.6
            // ---------------------------------------
            if (false)
            {
                SortedSet<string> ParenPermutations = Ex_9_6.GetAllParenthesesPermutations(5);
                textBox1.Text = CreateString(ParenPermutations);
                this.Text = ParenPermutations.Count + " Permutations";
            }

            // 9.8
            // ---------------------------------------
            if (false)
            {
                int numWays = Ex_9_8.GetNumWays(25, 100);
                textBox1.Text = numWays + " way(s).";
                //this.Text = ParenPermutations.Count + " Permutations";
            }

            // MergeSort
            // ---------------------------------------
            if (false)
            {
                int[] array = new int[10];
                //array[0] = 3;
                //array[1] = 10;
                //array[2] = 1;
                //array[3] = 34;
                //array[4] = 23;
                //array[5] = 15;
                //array[6] = 9;
                //array[7] = 54;
                //array[8] = 7;
                //array[9] = 4;
                //array[10] = 5;

                array[0] = 5;
                array[1] = 6;
                array[2] = 7;
                array[3] = 8;
                array[4] = 9;
                array[5] = 0;
                array[6] = 1;
                array[7] = 2;
                array[8] = 3;
                array[9] = 4;

                //for (int i = 0; i < array.Length; i++) array[i] = i;

                MergeSort.Sort(array);
                //QuickSort.Sort(array);

                textBox1.Text = CreateString(array);
            }

            int a = 10;
            int b = 20;
            Swap(ref a, ref b);

            textBox1.SelectionStart = 0;
            textBox1.SelectionLength = 0;
        }

        public static void Swap(ref int a, ref int b)
        {
            // swap two ints in place (no temporary storage)
            a ^= b;
            b ^= a;
            a ^= b;
        }

        public static string CreateString<T>(IEnumerable<T> Collection)
        {
            StringBuilder stringBuilder = new StringBuilder();
            foreach (T obj in Collection)
            {
                stringBuilder.Append(obj.ToString() + Environment.NewLine);
            }

            return stringBuilder.ToString();
        }
    }
}
